<?php
/**
 * Created by PhpStorm.
 * User: Steven
 * Date: 2017/6/14
 * Time: 11:33
 */

namespace app\financial\model;


use think\Db;
use think\Model;

class OrderModel extends Model
{
    public function __construct($data = [])
    {
        parent::__construct($data);
    }

    /**
     * 获取订单列表
     * @param array $filter
     * @param array $order
     * @param int $limit
     * @param $query
     * @return \think\Paginator
     */
    public function getOrder($filter=[], $order=[], $limit=10, $query)
    {
        //默认条件
        $where = [
            'o.delete_time' => ['=', 0],
        ];
        if(!empty($filter) && is_array($filter)){
            $where = array_merge($where,$filter);
        }

        //默认排序
        $sort = ['o.id'=>'desc'];
        if(!empty($order) && is_array($order)){
            $sort = array_merge($order,$sort);
        }

        //分页url额外参数
        $page_query['query'] = [];
        if(!empty($query) && is_array($query)){
            $page_query['query'] = $query;
        }

        $data = $this->alias('o')
            ->field('o.*,t.name as tname,sc.name as school_name,cr.course_name,us.realname as operator,u.realname,
            u.user_login as parent_name,u.mobile,u.address,b.payee,b.bill_type,b.tax_code,b.address,b.phone,b.bank,b.account,b.obtain_type')
            ->join('class cl','o.class_id=cl.id')
            ->join('school sc','cl.school_id=sc.id')
            ->join('course cr','cl.course_id=cr.id')
            ->join('student t','o.student_id=t.id')
            ->join('user u','o.parent_id=u.id')
            ->join('bill b','o.bill_id=b.id','left')
            ->join('user us','o.operator=us.id','left')
            ->where($where)
            ->order($sort)
            ->paginate($limit,false,$page_query);

        return $data;
    }

    /**
     * 编辑订单
     * @param array $data
     * @return bool
     */
    public function editOrder($data=[])
    {
        $result = true;
        $id = intval($data['id']);

        $oldOrder = $this->where('id', $id)->find();
        if(!isset($oldOrder->id)){
            $result = false;
        }else{
            self::startTrans();
            try{
                $this->isUpdate(true)->allowField(true)->save($data, ['id' => $id]);

                self::commit();
            }catch(\Exception $e){
                self::rollback();
                $result = false;
            }
        }

        return $result;
    }

    /**
     * 通过字段获取订单
     * @param array $filter
     * @return array|false|\PDOStatement|string|Model
     */
    public function getOrderByField($filter=[])
    {
        //默认条件
        $where = [
            'o.delete_time' => ['eq', 0] //未删除
        ];
        if(!empty($filter) && is_array($filter)){
            $where = array_merge($where,$filter);
        }

        $obj = $this->alias('o')
            ->field('o.*,t.name as student_name,sc.name as school_name,cr.course_name,us.realname as operator,u.realname,
            u.user_login as parent_name,u.mobile,u.address,b.payee,b.bill_type,b.tax_code,b.address,b.phone,b.bank,b.account,b.obtain_type')
            ->join('class cl','o.class_id=cl.id')
            ->join('school sc','cl.school_id=sc.id')
            ->join('course cr','cl.course_id=cr.id')
            ->join('student t','o.student_id=t.id')
            ->join('user u','o.parent_id=u.id')
            ->join('bill b','o.bill_id=b.id','left')
            ->join('user us','o.operator=us.id','left')
            ->where($where)
            ->find();

        return $obj;
    }

    /**
     * 得到唯一的订单编号
     * @params null
     * @return string 订单编号
     */
    public function generate_code()
    {
        $i = mt_rand(0,9999);
        do{
            if($i == 9999){
                $i = 0;
            }
            $i++;

            $order_id = date('YmdHis').str_pad($i,4,'0',STR_PAD_LEFT);
            //默认条件
            $where = [
                'order_id' => ['eq', $order_id],
            ];
            $row = $this->where($where)->find();
        }while($row);

        return $order_id;
    }
}